/*
 * @Description: 
 * @Author: duan_shuangjun 丶
 * @Date: 2025-08-20 17:22:03
 * @LastEditTime: 2025-09-24 22:34:29
 */
/*
 * @Description: 
 * @Author: duan_shuangjun 丶
 * @Date: 2025-08-20 17:22:03
 * @LastEditTime: 2025-08-21 13:44:58
 */
import { createRouter, createWebHistory, createMemoryHistory } from 'vue-router'
import type { RouteRecordRaw } from 'vue-router'
import { LocalStorage } from '@/utils/storage'
import appraiseRouter from './modules/appraise'
import billRouter from './modules/bill'
import bookRouter from './modules/book'
import memberRouter from './modules/member'
import moreRouter from './modules/more'
import HomeIndex from '@/views/Home/Index.vue'
import LoginIndex from '@/views/Login/Index.vue'
import DiancaiIndex from '@/views/Diancai/Index.vue'
import FoodIndex from '@/views/Home/food.vue'

const routes: RouteRecordRaw[] = [
    {
        path: '/',
        redirect: '/login'
    },
    {
        path: '/login',
        name: 'Login',
        component: LoginIndex
    },
    {
        path: '/home',
        component: () => import('@/layout/Index.vue'),
        children: [
            { path: '/home', component: HomeIndex }
        ]
    },
    {
        path: '/home/food/:id',
        component: () => import('@/layout/Index.vue'),
        children: [
            { path: '/home/food/:id', component: FoodIndex }
        ]
    },
    {
        path: '/home/diancai/:id',
        component: () => import('@/layout/Index.vue'),
        children: [
            { path: '/home/diancai/:id', name: 'FoodOrder', component: DiancaiIndex }
        ]
    },
    ...appraiseRouter,
    ...billRouter,
    ...bookRouter,
    ...memberRouter,
    ...moreRouter
]

// 检测是否在Electron环境中
const isElectron = typeof window !== 'undefined' && window.process && window.process.type === 'renderer'

const router = createRouter({
    history: isElectron ? createMemoryHistory() : createWebHistory(),
    routes
})

// 路由守卫 - 处理认证和自动跳转
router.beforeEach((to, from, next) => {
    const token = LocalStorage.get('token')

    console.log('路由守卫触发:', {
        to: to.path,
        from: from.path,
        hasToken: !!token,
        isElectron: isElectron
    })

    // 处理非标准路径（如文件路径）
    if ((to.path.includes('.html') || to.path.includes('app.asar'))) {
        console.log('检测到文件路径，重定向到根路径')
        next('/')
        return
    }

    // 如果访问根路径，根据token状态跳转
    if (to.path === '/') {
        if (token) {
            console.log('根路径访问，有token，跳转到首页')
            next('/home')
        } else {
            console.log('根路径访问，无token，跳转到登录页')
            next('/login')
        }
        return
    }

    // 如果没有token且不是登录页，跳转到登录页
    if (!token && to.path !== '/login') {
        console.log('路由守卫：无token，跳转到登录页')
        next('/login')
        return
    }

    // 如果有token且是登录页，跳转到首页
    if (token && to.path === '/login') {
        console.log('路由守卫：有token但访问登录页，跳转到首页')
        next('/home')
        return
    }

    console.log('路由守卫：允许访问', to.path)
    next()
})

export default router
